package api

import (
	"awesomeProject2/global"
	"awesomeProject2/model"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func GetAllNumber(c *gin.Context) {
	year, _ := strconv.Atoi(c.Param("year"))
	data := model.GetAllNumber(year)
	c.JSON(http.StatusOK, gin.H{
		"data": data,
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	},
	)
}
func GetAllPaperInfo(c *gin.Context) {
	year, _ := strconv.Atoi(c.Param("year"))
	data := model.GetPaperDetail(year)
	c.JSON(http.StatusOK, gin.H{
		"data": data,
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	},
	)
}
func GetAllPatentInfo(c *gin.Context) {
	year, _ := strconv.Atoi(c.Param("year"))
	data := model.GetPatentDetail(year)
	c.JSON(http.StatusOK, gin.H{
		"data": data,
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	},
	)
}
func GetAllAcInfo(c *gin.Context) {
	year, _ := strconv.Atoi(c.Param("year"))
	data := model.GetAcDetail(year)
	c.JSON(http.StatusOK, gin.H{
		"data": data,
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	})
}
func GetAllPjInfo(c *gin.Context) {
	year, _ := strconv.Atoi(c.Param("year"))
	data := model.GetPJDetail(year)
	c.JSON(http.StatusOK, gin.H{
		"data": data,
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	})
}
func GetAllWKInfo(c *gin.Context) {
	year, _ := strconv.Atoi(c.Param("year"))
	data := model.GetWKDetail(year)
	c.JSON(http.StatusOK, gin.H{
		"data": data,
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	})
}
func GetFiveInfo(c *gin.Context) {
	year, _ := strconv.Atoi(c.Param("year"))
	data := model.GetFiveYearsWks(year)
	c.JSON(http.StatusOK, gin.H{
		"data": data,
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	})
}

type returndata struct {
	Name string  `json:"name"`
	Data []int64 `json:"data"`
}

func GetPersonInfo(c *gin.Context) {
	userid, _ := c.Get("userid")
	var paperdata []returndata
	var projectdata []returndata
	acm := model.GetPersonACMPaperInfo(userid.(uint))
	sci := model.GetPersonSCIPaperInfo(userid.(uint))
	ccf := model.GetPersonCCFPaperInfo(userid.(uint))

	paperdata = []returndata{
		{"ACM/IEEE Trans", acm},
		{"SCI一区", sci},
		{"CCF A", ccf},
	}
	guozhong := model.GetPersonProject(userid.(uint), "国家重点")
	gj := model.GetPersonProject(userid.(uint), "国家级")
	sb := model.GetPersonProject(userid.(uint), "省部级")
	qy := model.GetPersonProject(userid.(uint), "企业")
	qt := model.GetPersonProject(userid.(uint), "其他")
	projectdata = []returndata{
		{"国家重点项目", guozhong},
		{"国家项目", gj},
		{"省部项目", sb},
		{"企业项目", qy},
		{"其他项目", qt},
	}
	var workdata []returndata
	wkgj := model.GetPersonWk(userid.(uint), "国家级")
	wksb := model.GetPersonWk(userid.(uint), "省部级")
	workdata = []returndata{
		{"国家级专著", wkgj},
		{"省部级专著", wksb},
	}
	var acdata []returndata
	acgj := model.GetPersonAC(userid.(uint), "国家级")
	acsb := model.GetPersonAC(userid.(uint), "省部级")
	actj := model.GetPersonAC(userid.(uint), "厅局级")
	acdata = []returndata{
		{"国家级", acgj},
		{"省部级", acsb},
		{"厅局级", actj},
	}
	var patentdata []returndata
	ptfm := model.GetPersonPT(userid.(uint), "发明专利")
	ptsy := model.GetPersonPT(userid.(uint), "实用新型")
	ptwg := model.GetPersonPT(userid.(uint), "外观专利")
	patentdata = []returndata{
		{"发明专利", ptfm},
		{"实用新型", ptsy},
		{"外观专利", ptwg},
	}
	Year := model.GetYear()
	c.JSON(http.StatusOK, gin.H{
		"data": gin.H{
			"paper":       gin.H{"data": paperdata},
			"project":     gin.H{"data": projectdata},
			"work":        gin.H{"data": workdata},
			"achievement": gin.H{"data": acdata},
			"patent":      gin.H{"data": patentdata},
			"common":      gin.H{"year": []int{Year - 5, Year - 4, Year - 3, Year - 2, Year - 1, Year}},
		},
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	})
}

//获取个人的patent图表信息
func GetPersonInfoAdmin(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var tc model.Teacher
	global.GVA_DB.Model(&model.Teacher{}).Where("id = ?", uint(id)).First(&tc)
	userid := tc.UserId
	var paperdata []returndata
	var projectdata []returndata
	acm := model.GetPersonACMPaperInfo(userid)
	sci := model.GetPersonSCIPaperInfo(userid)
	ccf := model.GetPersonCCFPaperInfo(userid)

	paperdata = []returndata{
		{"ACM/IEEE Trans", acm},
		{"SCI一区", sci},
		{"CCF A", ccf},
	}
	guozhong := model.GetPersonProject(userid, "国家重点")
	gj := model.GetPersonProject(userid, "国家级")
	sb := model.GetPersonProject(userid, "省部级")
	qy := model.GetPersonProject(userid, "企业")
	qt := model.GetPersonProject(userid, "其他")
	projectdata = []returndata{
		{"国家重点项目", guozhong},
		{"国家项目", gj},
		{"省部项目", sb},
		{"企业项目", qy},
		{"其他项目", qt},
	}
	var workdata []returndata
	wkgj := model.GetPersonWk(userid, "国家级")
	wksb := model.GetPersonWk(userid, "省部级")
	workdata = []returndata{
		{"国家级专著", wkgj},
		{"省部级专著", wksb},
	}
	var acdata []returndata
	acgj := model.GetPersonAC(userid, "国家级")
	acsb := model.GetPersonAC(userid, "省部级")
	actj := model.GetPersonAC(userid, "厅局级")
	acdata = []returndata{
		{"国家级", acgj},
		{"省部级", acsb},
		{"厅局级", actj},
	}
	var patentdata []returndata
	ptfm := model.GetPersonPT(userid, "发明专利")
	ptsy := model.GetPersonPT(userid, "实用新型")
	ptwg := model.GetPersonPT(userid, "外观专利")
	patentdata = []returndata{
		{"发明专利", ptfm},
		{"实用新型", ptsy},
		{"外观专利", ptwg},
	}
	Year := model.GetYear()
	c.JSON(http.StatusOK, gin.H{
		"data": gin.H{
			"paper":       gin.H{"data": paperdata},
			"project":     gin.H{"data": projectdata},
			"work":        gin.H{"data": workdata},
			"achievement": gin.H{"data": acdata},
			"patent":      gin.H{"data": patentdata},
			"common":      gin.H{"year": []int{Year - 5, Year - 4, Year - 3, Year - 2, Year - 1, Year}},
		},
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	})
}
func GetPersonPatent(c *gin.Context) {
	userid, _ := c.Get("userid")

	var patentdata []returndata
	ptfm := model.GetPersonPT(userid.(uint), "发明专利")
	ptsy := model.GetPersonPT(userid.(uint), "实用新型")
	ptwg := model.GetPersonPT(userid.(uint), "外观专利")
	patentdata = []returndata{
		{"发明专利", ptfm},
		{"实用新型", ptsy},
		{"外观专利", ptwg},
	}
	Year := model.GetYear()
	c.JSON(http.StatusOK, gin.H{
		"data": gin.H{

			"patent": gin.H{"data": patentdata},
			"common": gin.H{"year": []int{Year - 5, Year - 4, Year - 3, Year - 2, Year - 1, Year}},
		},
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	})
}
func GetPersonPatentAdmin(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var tc model.Teacher
	global.GVA_DB.Model(&model.Teacher{}).Where("id = ?", uint(id)).First(&tc)
	userid := tc.UserId

	var patentdata []returndata
	ptfm := model.GetPersonPT(userid, "发明专利")
	ptsy := model.GetPersonPT(userid, "实用新型")
	ptwg := model.GetPersonPT(userid, "外观专利")
	patentdata = []returndata{
		{"发明专利", ptfm},
		{"实用新型", ptsy},
		{"外观专利", ptwg},
	}
	Year := model.GetYear()
	c.JSON(http.StatusOK, gin.H{
		"data": gin.H{

			"patent": gin.H{"data": patentdata},
			"common": gin.H{"year": []int{Year - 5, Year - 4, Year - 3, Year - 2, Year - 1, Year}},
		},
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	})
}

type NameValue struct {
	Name  string  `json:"name"`
	Value float64 `json:"value"`
}
type PjmoneyReturnInfo struct {
	Name     string      `json:"name"`
	Value    int64       `json:"value"`
	Children []NameValue `json:"children"`
}
type NameChildren struct {
	Name     string              `json:"name"`
	Children []PjmoneyReturnInfo `json:"children"`
}

func GetAllPjMoney(c *gin.Context) {
	year, _ := strconv.Atoi(c.Param("year"))
	var zxpj NameChildren
	var hxpj NameChildren

	gzsz, a := model.GetAllPJMoney(year, "国家重点")
	gzmx := []NameValue{
		{"立项经费", gzsz[1]},
		{"实到经费", gzsz[0]},
		{"累计经费", gzsz[2]},
	}
	gz := PjmoneyReturnInfo{
		Name:     "国家重点",
		Value:    a,
		Children: gzmx,
	}
	gjsz, b := model.GetAllPJMoney(year, "国家级")
	gjmx := []NameValue{
		{"立项经费", gjsz[1]},
		{"实到经费", gjsz[0]},
		{"累计经费", gjsz[2]},
	}
	gj := PjmoneyReturnInfo{
		Name:     "国家级",
		Value:    b,
		Children: gjmx,
	}
	sbsz, sbsm := model.GetAllPJMoney(year, "省部级")
	sbmx := []NameValue{
		{"立项经费", sbsz[1]},
		{"实到经费", sbsz[0]},
		{"累计经费", sbsz[2]},
	}
	sb := PjmoneyReturnInfo{
		Name:     "省部级",
		Value:    sbsm,
		Children: sbmx,
	}
	qtsz, qtsm := model.GetAllPJMoney(year, "其他")
	qtmx := []NameValue{
		{"立项经费", qtsz[1]},
		{"实到经费", qtsz[0]},
		{"累计经费", qtsz[2]},
	}
	qt := PjmoneyReturnInfo{
		Name:     "其他",
		Value:    qtsm,
		Children: qtmx,
	}
	qysz, qysm := model.GetAllPJMoney(year, "企业")
	qymx := []NameValue{
		{"立项经费", qysz[1]},
		{"实到经费", qysz[0]},
		{"累计经费", qysz[2]},
	}
	qy := PjmoneyReturnInfo{
		Name:     "企业",
		Value:    qysm,
		Children: qymx,
	}
	hxpj = NameChildren{
		Name:     "横向项目",
		Children: []PjmoneyReturnInfo{qy},
	}
	zxpj = NameChildren{
		Name:     "纵向项目",
		Children: []PjmoneyReturnInfo{gz, gj, sb, qt},
	}
	data_return_info := []NameChildren{zxpj, hxpj}
	c.JSON(http.StatusOK, gin.H{
		"data": data_return_info,
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	},
	)
}
func GetAllPjMoneyPerson(c *gin.Context) {
	year, _ := strconv.Atoi(c.Param("year"))

	useriddata, _ := c.Get("userid")
	id := useriddata.(uint)
	var zxpj NameChildren

	var hxpj NameChildren

	gzsz, a := model.GetAllPJMoneyPerson(id, year, "国家重点")
	gzmx := []NameValue{
		{"立项经费", gzsz[1]},
		{"实到经费", gzsz[0]},
		{"累计经费", gzsz[2]},
	}
	gz := PjmoneyReturnInfo{
		Name:     "国家重点",
		Value:    a,
		Children: gzmx,
	}
	gjsz, b := model.GetAllPJMoneyPerson(id, year, "国家级")
	gjmx := []NameValue{
		{"立项经费", gjsz[1]},
		{"实到经费", gjsz[0]},
		{"累计经费", gjsz[2]},
	}
	gj := PjmoneyReturnInfo{
		Name:     "国家级",
		Value:    b,
		Children: gjmx,
	}
	sbsz, sbsm := model.GetAllPJMoneyPerson(id, year, "省部级")
	sbmx := []NameValue{
		{"立项经费", sbsz[1]},
		{"实到经费", sbsz[0]},
		{"累计经费", sbsz[2]},
	}
	sb := PjmoneyReturnInfo{
		Name:     "省部级",
		Value:    sbsm,
		Children: sbmx,
	}
	qtsz, qtsm := model.GetAllPJMoneyPerson(id, year, "其他")
	qtmx := []NameValue{
		{"立项经费", qtsz[1]},
		{"实到经费", qtsz[0]},
		{"累计经费", qtsz[2]},
	}
	qt := PjmoneyReturnInfo{
		Name:     "其他",
		Value:    qtsm,
		Children: qtmx,
	}
	qysz, qysm := model.GetAllPJMoneyPerson(id, year, "企业")
	qymx := []NameValue{
		{"立项经费", qysz[1]},
		{"实到经费", qysz[0]},
		{"累计经费", qysz[2]},
	}
	qy := PjmoneyReturnInfo{
		Name:     "企业",
		Value:    qysm,
		Children: qymx,
	}
	hxpj = NameChildren{
		Name:     "横向项目",
		Children: []PjmoneyReturnInfo{qy},
	}
	zxpj = NameChildren{
		Name:     "纵向项目",
		Children: []PjmoneyReturnInfo{gz, gj, sb, qt},
	}
	data_return_info := []NameChildren{zxpj, hxpj}
	c.JSON(http.StatusOK, gin.H{
		"data": data_return_info,
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	},
	)
}

//通过json传递
type TempJson struct {
	Year int  `json:"year"`
	ID   uint `json:"id"`
}

func GetAllPjMoneyPersonClickByAdmin(c *gin.Context) {

	idinfo, _ := strconv.Atoi(c.Param("id"))
	year, _ := strconv.Atoi(c.Param("year"))
	var tc model.Teacher
	global.GVA_DB.Model(&model.Teacher{}).Where("id = ?", uint(idinfo)).First(&tc)
	id := tc.UserId
	var zxpj NameChildren
	var hxpj NameChildren

	gzsz, a := model.GetAllPJMoneyPerson(id, year, "国家重点")
	gzmx := []NameValue{
		{"立项经费", gzsz[1]},
		{"实到经费", gzsz[0]},
		{"累计经费", gzsz[2]},
	}
	gz := PjmoneyReturnInfo{
		Name:     "国家重点",
		Value:    a,
		Children: gzmx,
	}
	gjsz, b := model.GetAllPJMoneyPerson(id, year, "国家级")
	gjmx := []NameValue{
		{"立项经费", gjsz[1]},
		{"实到经费", gjsz[0]},
		{"累计经费", gjsz[2]},
	}
	gj := PjmoneyReturnInfo{
		Name:     "国家级",
		Value:    b,
		Children: gjmx,
	}
	sbsz, sbsm := model.GetAllPJMoneyPerson(id, year, "省部级")
	sbmx := []NameValue{
		{"立项经费", sbsz[1]},
		{"实到经费", sbsz[0]},
		{"累计经费", sbsz[2]},
	}
	sb := PjmoneyReturnInfo{
		Name:     "省部级",
		Value:    sbsm,
		Children: sbmx,
	}
	qtsz, qtsm := model.GetAllPJMoneyPerson(id, year, "其他")
	qtmx := []NameValue{
		{"立项经费", qtsz[1]},
		{"实到经费", qtsz[0]},
		{"累计经费", qtsz[2]},
	}
	qt := PjmoneyReturnInfo{
		Name:     "其他",
		Value:    qtsm,
		Children: qtmx,
	}
	qysz, qysm := model.GetAllPJMoneyPerson(id, year, "企业")
	qymx := []NameValue{
		{"立项经费", qysz[1]},
		{"实到经费", qysz[0]},
		{"累计经费", qysz[2]},
	}
	qy := PjmoneyReturnInfo{
		Name:     "企业",
		Value:    qysm,
		Children: qymx,
	}
	hxpj = NameChildren{
		Name:     "横向项目",
		Children: []PjmoneyReturnInfo{qy},
	}
	zxpj = NameChildren{
		Name:     "纵向项目",
		Children: []PjmoneyReturnInfo{gz, gj, sb, qt},
	}
	data_return_info := []NameChildren{zxpj, hxpj}
	c.JSON(http.StatusOK, gin.H{
		"data": data_return_info,
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	},
	)
}
